//
//  STSmileFaceViewController.swift
//  BasicAnimation-Swift
//
//  Created by Stan on 2017-02-12.
//  Copyright © 2017 stan. All rights reserved.
//

import UIKit

class STSmileFaceViewController: UIViewController {
    
    @IBOutlet weak var yellowView: UIView!
    
    @IBOutlet weak var greenView: UIView!
    
    @IBOutlet weak var mouseView: UIView!
    
    @IBOutlet weak var titleLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //      设置头部标题的文字
        self.titleLabel.text = "吓死宝宝了！"
        
        //        暂时先隐藏头部标题，等动画播放完成再显示
        self.titleLabel.isHidden = true
    }
    
    //    触摸屏之后触发的方法
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        //        添加阻尼动画
        UIView.animate(withDuration: 0.8, delay: 0.2, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.8, options: UIViewAnimationOptions(), animations: {
            
            //            改变黄色的中心点位置
            self.yellowView.center.x = self.view.bounds.width - self.yellowView.center.x
            self.yellowView.center.y = self.yellowView.center.y + 30
            
            //            改变蓝色的中心点位置
            self.greenView.center.x = self.view.bounds.width - self.greenView.center.x
            self.greenView.center.y = self.greenView.center.y + 30
            
            //            改变嘴巴的高度和中心点y的位置
            self.mouseView.frame.size.height = 180
            self.mouseView.center.y = self.mouseView.center.y + 30
            
            //            这句一定要加，不然看不到动画
            self.view.layoutIfNeeded()
            
            
        }, completion:  { (result) in
            
            //            如果上面的动画执行完成，执行下面的动画
            if result{
                
                //                添加阻尼动画
                UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.8, options: UIViewAnimationOptions(), animations: {
                    
                    //                    把隐藏的标题文字设置为显示状态
                    self.titleLabel.isHidden = false
                    
                }, completion: nil)
                

            }
        })

    }
    
}
